package com.dateview.test.util;

import java.util.Calendar;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description:日期工具类
 */
public class DateUtil {
    private String[] weekdays = {"日", "一", "二", "三", "四", "五", "六"};

    /**
     * 获取月份
     *
     * @return
     */
    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     * 获取年
     *
     * @return
     */
    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取月份
     *
     * @return
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当月的 天数
     *
     * @return
     */
    public static int getCurrentMonthDays() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据月 获取对应的月份 天数
     */
    public static int getDaysByMonth(int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, a.get(Calendar.YEAR));
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取指定日期是某月的星期几
     *
     * @return
     */
    public static int getWeekDayInAMonth(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 根据月 获取对应的月份第一天是星期几
     *
     * @return
     */
    public static int getTheFirstWeekDayInAMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return getWeekDayInAMonth(year, month, 1);
    }
}
